package com.rumor.background.Utils;

import javax.servlet.http.HttpServletRequest;

import com.rumor.background.pojo.RumorContributeBackground;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.*;
import java.util.Iterator;
import java.util.Random;

public class Utils {

    // 获得rumor投稿的随机编号
    public static String getRumorCode(){
        return 'r' + getRandCode(7);
    }

    // 获得失物招领的随机编号
    public static String getFoundCode(){
        return 'f' + getRandCode(7);
    }

    // 获得寻物启事的随机编号
    public static String getLostCode(){
        return 'l' + getRandCode(7);
    }

    // 生成随机用户名
    public static String getUsername(){
        return "user-" + getRandCode(12);
    }

    // 生成管理员动态码
    public static String getAdminCode(){
        return "admin-" + getRandCode(12);
    }

    // 生成n位随机编号
    private static String getRandCode(int n){
        StringBuilder rand = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            // 输出字母还是数字
            boolean charOrNum = random.nextInt(2) % 2 == 0;
            // 字符串
            if (charOrNum) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                rand.append((char) (choice + random.nextInt(26)));
            } else { // 数字
                rand.append(String.valueOf(random.nextInt(10)));
            }
        }
        return rand.toString();
    }

    // 保存图片文件
    public static void saveImgFile(HttpServletRequest request, String path, String filename) throws IOException {
        StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;

        // 判断存放上传文件的目录是否存在（不存在则创建）
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 保存图片
        Iterator<String> iterator = req.getFileNames();
        while(iterator.hasNext()){
            MultipartFile file=req.getFile(iterator.next());
            File img = new File(path + filename + "." + "jpg");
            if (!img.exists()) {
                img.getParentFile().mkdirs();
            }
            OutputStream out=new FileOutputStream(img);
            out.write(file.getBytes());
            out.close();
        }
        System.out.println("==============图片保存于" + path + "========================");
    }

    // 删除图片文件
    public static int deleteImgFile(String path, String filename){
        File file = new File(path + "/" + filename);
        if (!file.exists()) {  // 不存在返回 false
            return 1;
        } else {
            // 判断是否为文件
            if (file.isFile()) {  // 为文件时调用删除文件方法
                file.delete();
                return 1;
            }
        }
        return -1;
    }

    // 获得path下的所有图片的链接
    public static String[] getImgListLink(HttpServletRequest request, String path) throws IOException {
        File directory = new File(path);
        File[] fs = directory.listFiles();
        assert fs != null;
        String[] list = new String[fs.length];
        for(int i = 0; i < fs.length; i++){
            list[i] = fs[i].getPath();
        }
        String link = request.getScheme() + "://" + request.getServerName() + ":"  + "8081" + "/api/";
        for(int i = 0; i < list.length; i++){
            list[i] = link + list[i].replace("\\", "/");
        }
        return list;
    }

}
